<template>
  <div>
    <el-header>
      <div class="head_div">
        <img class="head_log" src="../assets/haha.png"/>
        <span>荒天帝后台管理系统</span>
        <el-button class="head_button" type="primary">退出</el-button>
      </div>
    </el-header>

    <el-container>
      <!-- 侧边栏菜单区域 -->
      <el-aside>
        <div class="toggle" :class="{active:isActive}" @click="toggle_aside($event)">收缩</div>
        <!--
        :router 以index作为跳转路径
        unique-opened每次只展开一个
        collapse是折叠,配合上面的:class="{active:isActive}
        collapse-transition 关闭折叠展开动画(更流畅)
        -->
        <el-menu :router="true" :unique-opened="false" :collapse="isCollapse" :collapse-transition="false" :default-active="default_active">
          <!-- 一级菜单 -->
          <el-submenu :index='item.id+""' v-for="item in menulist" :key="item.id">
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <!--          因为以index作为跳转,这里必须转换为字符串-->
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id"
                          @click="saveNavState('/' + subItem.path)">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!--      右侧内容主体-->
      <el-main>
         <router-view></router-view>
      </el-main>
    </el-container>
  </div>
</template>

<script>
  export default {
    name: "home",
    data() {
      return {
        isCollapse:false,
        isActive:false,
        // 填充menu的index
        default_active:'',
        //TODO
        menulist: [
          {
            id:"1",
            authName:"产品管理",
            children:[
                {id:"2",authName:"分类管理",path:"PmsCategory"},
                {id:"3",authName:"品牌管理",path:"PmsBrand"},
              ]
          },
          {id:"4",authName:"属性管理",children:[
              {id:"5",authName:"属性分组管理",path:"PmsAttrGroup"},
              {id:"6",authName:"规格参数管理",path:"PmsAttr"},
            ]}

        ],
        iconsObj: {
          "1": "el-icon-user-solid",
          "2": "el-icon-s-tools",
          "3": "el-icon-s-cooperation",
          "4": "el-icon-s-home",
          "6": "el-icon-s-home",
          "7": "el-icon-s-home",
          "8": "el-icon-s-home",
          "9": "el-icon-s-home",
          "10": "el-icon-s-home",
        }
      }
    },
    methods: {
      saveNavState(path)
      {
           //保存激活的路径,也就是点击了哪个导航,刷新后还有
          window.sessionStorage.setItem("default_active",path);
          this.default_active=path;
      },
      // 从后台查询菜单,create里注释了,模版的话肯定是用自定义的
      findAllmenus()
      {
        this.$http.get("menus").then((res) => {
          this.menulist = res.data;
          //这里做了响应的异常拦截,如果不是200返回值,那么就会异常
        }).catch(res => {
          this.$message.error(res + "===列表获取失败")
        })
      },
      toggle_aside()
      {
        this.isCollapse=!this.isCollapse;
        this.isActive=!this.isActive;
      }
    },
    created(event) {
      // this.findAllmenus();
      //激活
      //保存激活
      var item = window.sessionStorage.getItem("default_active");
      this.default_active=item;
    }
  }
</script>

<style scoped>
  .head_log {
    vertical-align: middle;
    margin-right: 20px;
  }

  .head_button {
    float: right;
    margin-top: 10px;
  }

  .head_div {
    /*margin-top: 10px;*/
  }

  .el-header {
    font: bold 20px '微软雅黑';
    color: white;
    background-color: rgba(0, 0, 0, 0.9);
  }

  .toggle {
    text-align: center;
    font-size: 20px;
    color: white;
    background-color: gray;
    letter-spacing: 0.5em;;
    cursor: pointer;
  }

  .active{
      width: 65px;
  }

</style>
